package com.example.dllo.baidumusic.fragment;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.dllo.baidumusic.R;
import com.example.dllo.baidumusic.adapter.MenuAdapter;
import com.example.dllo.baidumusic.adapter.OnitenClickListener;
import com.example.dllo.baidumusic.bean.MenuBean;
import com.example.dllo.baidumusic.nettool.URLValuas;
import com.example.dllo.baidumusic.main.NewActivity;
import com.example.dllo.baidumusic.nettool.BaseFragment;
import com.example.dllo.baidumusic.nettool.NetTool;
import com.example.dllo.baidumusic.events.UpUrlEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by dllo on 16/8/15.
 */
public class MenuFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private MenuAdapter adapterMenu;
    private GridLayoutManager manager;

    @Override
    protected int setLayout() {
        return R.layout.fragment_menu;
    }

    @Override
    protected void initView() {
        recyclerView = bindView(R.id.menu_fragment_recyview);
        adapterMenu = new MenuAdapter(getContext());
        manager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(manager);


    }

    @Override
    protected void initData() {
        NetTool netTool = new NetTool();
        netTool.getNetData(URLValuas.ALL_SONGLIST, MenuBean.class, new NetTool.NetListener<MenuBean>() {
            @Override
            public void onSuccess(MenuBean menuBean) {
                adapterMenu.setArrayList(menuBean);
                recyclerView.setAdapter(adapterMenu);
                UpData(menuBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    private void UpData(final MenuBean menuBean) {
           adapterMenu.setOnitenClickListener(new OnitenClickListener() {
               @Override
               public void onClick(int itemPostion) {
                   String url = menuBean.getContent().get(itemPostion).getListid();
                   String aUrl = URLValuas.SONGLIST_DETAIL_Front + url + URLValuas.SONGLIST_DETAIL_BEHIND;

                   UpUrlEvent upUrlEvent = new UpUrlEvent();
                   upUrlEvent.setURL(aUrl);
                   EventBus.getDefault().post(upUrlEvent);
                   SingDanListFragment singDanListFragment = new SingDanListFragment();
                   Bundle bundle = new Bundle();
                   bundle.putString("XXX",aUrl);
                   singDanListFragment.setArguments(bundle);
                   NewActivity.getManager(singDanListFragment);

               }
           });

    }


}
